package gpl.scotlandyard.utils;

import gpl.scotlandyard.ui.tools.JDialogs;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * I18n internationalization.
 * <p>Base name used for bundel : <pre>BUNDLE_NAME = Config.get("I18N")</pre><p/>
 * <p>If any error occured during loading of bundle exit with :<pre>System.exit(Main.FATAL_I18N)</pre></p>
 * @author Norbert Martin
 */
public abstract class I18n {

  private static final Logger LOG = Logger.getLogger(I18n.class);
  private static final String BUNDLE_NAME = Config.get("I18N");

  private static ResourceBundle RESOURCE_BUNDLE;
  static {
    try {
      RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
      LOG.info("I18n loaded");
    } catch (MissingResourceException e) {
      String msg = "Impossible to load langage resources in : " + BUNDLE_NAME;
      JDialogs.showError(null, msg);
      LOG.fatal(msg);
      System.exit(Constants.FATAL_I18N);
    }
  }

  /**
   * Returns the value attached to this key or null.
   * @param key to find the value
   * @return the value or null if no value find for this key
   * @throws NullPointerException if key is null
   * @throws MissingResourceException if no object for the given key can be found
   * @throws ClassCastException if the object found for the given key is not a string
   */
  public static String get(String key) {
    return RESOURCE_BUNDLE.getString(key);
  }
}
